package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.model.Option;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.DeptForm;
import com.dimples.dd.system.model.query.DeptPageQuery;
import com.dimples.dd.system.model.vo.DeptTreeVO;
import com.dimples.dd.system.service.SystemDeptService;
import com.dimples.dd.web.core.annotation.PreventDuplicateResubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/16
 */
@Tag(name = "04.部门接口")
@RestController
@Validated
@RequestMapping("dept")
@RequiredArgsConstructor
public class SysDeptController {

    private final SystemDeptService systemDeptService;

    @Operation(summary = "获取部门下拉选项")
    @GetMapping("/options")
    @PreAuthorize("@ss.hasPerm('system:dept:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public CommonResult<List<Option<Long>>> listDeptOptions() {
        List<Option<Long>> list = this.systemDeptService.listDeptOptions();
        return CommonResult.success(list);
    }

    @Operation(summary = "获取部门列表")
    @PreAuthorize("@ss.hasPerm('system:dept:query')")
    @GetMapping("list")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<DeptTreeVO> listDepartments(
            @ParameterObject DeptPageQuery queryParams
    ) {
        List<DeptTreeVO> list = this.systemDeptService.listDepartments(queryParams);
        return PageResult.success(list);
    }

    @Operation(summary = "获取部门表单数据")
    @GetMapping("/{deptId}/form")
    @PreAuthorize("@ss.hasPerm('system:dept:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<DeptForm> selectDeptForm(
            @Parameter(description = "部门ID") @PathVariable Long deptId
    ) {
        DeptForm deptForm = this.systemDeptService.selectDeptForm(deptId);
        return CommonResult.success(deptForm);
    }

    @Operation(summary = "新增部门")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:dept:create')")
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    @PreventDuplicateResubmit
    public CommonResult<Long> createDept(
            @Valid @RequestBody DeptForm formData
    ) {
        Long id = this.systemDeptService.createDept(formData);
        return CommonResult.success(id);
    }

    @Operation(summary = "修改部门")
    @PutMapping(value = "/{deptId}")
    @PreAuthorize("@ss.hasPerm('system:dept:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Long> updateDept(
            @PathVariable Long deptId,
            @Valid @RequestBody DeptForm formData
    ) {
        deptId = this.systemDeptService.updateDept(deptId, formData);
        return CommonResult.success(deptId);
    }

    @Operation(summary = "删除部门")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:dept:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteDepartments(
            @Parameter(description = "部门ID，多个以英文逗号(,)分割") @PathVariable("ids") String ids
    ) {
        boolean result = this.systemDeptService.deleteByIds(ids);
        return CommonResult.success(result);
    }

}
